package com.test.work.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.work.dto.AjaxMsgDto;
import com.test.work.dto.DataTable4DisplayDto;
import com.test.work.dto.DataTableDto;
import com.test.work.model.User;
import com.test.work.service.IAdminLoginService;

@Controller
@RequestMapping("/t_admin")
public class AdminLoginController {
	private static Logger logger = Logger.getLogger(AdminLoginController.class);

	@Resource
	private IAdminLoginService adminLoginService;

	/*1.提供一个服务器接口，安卓APP那边要做账号密码的登录。
	2，做页面， 总共两个网页，（1号网页）网页可以控制这些账号能否登录  ，（2号）网页，输入账号密码，正确后进入1号网页*/

	@RequestMapping("/login")
	@ResponseBody
	public AjaxMsgDto login(String username, String password) {
		AjaxMsgDto ajaxMsgDto = new AjaxMsgDto();
		try {
			ajaxMsgDto.setIsSuccess(true);
			Object info = adminLoginService.login(username,password);
			if(info != null){				
				ajaxMsgDto.setData(info);
			}
			else;
			logger.info(ajaxMsgDto.toString());
		} catch (Exception e) {
			e.printStackTrace();
			ajaxMsgDto.setIsSuccess(false);
		}
		return ajaxMsgDto;
	}
	
	@RequestMapping("/getTable")
	@ResponseBody
	public DataTable4DisplayDto getFoodTable(DataTableDto dataTableDto, Integer flag){
		DataTable4DisplayDto result = null;
		try {
			Integer count = this.adminLoginService.getfInfoCount(dataTableDto.getSearch(), flag);
			List<User> data = this.adminLoginService.getfInfoDataTable(dataTableDto, flag);
			result=new DataTable4DisplayDto(dataTableDto.getDraw(), count, count, data);
			logger.info(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxMsgDto delete(String ids) {
		AjaxMsgDto ajaxMsgDto = new AjaxMsgDto();
		try {
			adminLoginService.delete(ids);
			ajaxMsgDto.setIsSuccess(true);
			logger.info(ajaxMsgDto.toString());
		} catch (Exception e) {
			e.printStackTrace();
			ajaxMsgDto.setIsSuccess(false);
		}
		return ajaxMsgDto;
	}
	
	@RequestMapping("/recover")
	@ResponseBody
	public AjaxMsgDto recover(String ids) {
		AjaxMsgDto ajaxMsgDto = new AjaxMsgDto();
		try {
			adminLoginService.recover(ids);
			ajaxMsgDto.setIsSuccess(true);
			logger.info(ajaxMsgDto.toString());
		} catch (Exception e) {
			e.printStackTrace();
			ajaxMsgDto.setIsSuccess(false);
		}
		return ajaxMsgDto;
	}
	
}
